/**
 * Copyright (c) 2005 Frank Toennies
 * Redistributions in source code form must reproduce the above copyright and this condition.
 * The contents of this file are subject to the Sun Project JXTA License Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * A copy of the License is available at http://www.jxta.org/jxta_license.html.
 * Please see the license information page for instructions on use of the license in source files.
 *
 * This software consists of voluntary contributions made by many individuals on behalf of Project JXTA.
 * For more information on Project JXTA, please see http://www.jxta.org.
 *
 */
package net.jxta.myjxta.ui;

import net.jxta.myjxta.presence.PeerEntry;
import net.jxta.myjxta.presence.PresenceController;
import net.jxta.myjxta.util.Group;
import net.jxta.myjxta.util.JxtaNodeListener;
import net.jxta.myjxta.util.objectmodel.JxtaNode;
import net.jxta.myjxta.util.objectmodel.PeerNode;

import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;


/**
 * @author Frank Toennies --
 */
public class SingleGroupTableModel extends DefaultTableModel
        implements
        TableModel, JxtaNodeListener, PresenceController.PresenseListener {

    private final Group m_group;


    /**
     * @param peerGroup
     */
    public SingleGroupTableModel(Group peerGroup) {

        m_group = peerGroup;
        getPresenceController(m_group).registerListener(this);
    }

    public Group getGroup() {
        return m_group;
    }



    /* (non-Javadoc)
    * @see javax.swing.table.TableModel#getRowCount()
    */
    public int getRowCount() {
        if (getRows() != null) {
            return getRows().length;
        } else {
            return 0;
        }

    }

    /**
     * Returns <code>Object.class</code> regardless of <code>columnIndex</code>.
     *
     * @param columnIndex the column being queried
     * @return the Object.class
     */
    public Class<PeerEntry> getColumnClass(int columnIndex) {
        return PeerEntry.class;
    }

    /* (non-Javadoc)
      * @see javax.swing.table.TableModel#getColumnCount()
      */
    public int getColumnCount() {
        return 1;
    }

    /* (non-Javadoc)
      * @see javax.swing.table.TableModel#getValueAt(int, int)
      */
    public Object getValueAt(int row, int column) {
        if (row >= 0 && column >= 0) {
        	PeerEntry[] raw=getRows();
        	return raw[row];
        } else {
            return null;
        }
    }

    /* (non-Javadoc)
    * @see javax.swing.table.TableModel#getColumnName(int)
    */
    public String getColumnName(int colNr) {
        return null;
//		return STRINGS.getString("label.pipe.name");
    }

    public PeerNode[] getAllPeerNodes() {
        PeerNode result[] = new PeerNode[getRows().length];
        int i = 0;
        for (PeerEntry peerEntry : getRows()) {
            result[i] = peerEntry.getNode();
            i++;
        }
        return result;
    }

    public void shutdown() {
        for (PeerEntry peerEntry : getRows()) {
            peerEntry.getNode().removeListener(this);
        }
      }

    public PeerEntry[] getRows() {
        Group group = getGroup();
        if (group == null)
            return null;

        return getPresenceController(group).getAllContacts();
    }

    private PresenceController getPresenceController(Group p_group) {
        return PresenceController.getController(p_group.getId());
    }

    public void entryAdded(PeerEntry entry) {
        entry.getNode().addListener(this);
        fireTableDataChanged();
    }

    public void entryRemoved(PeerEntry entry) {
        entry.getNode().removeListener(this);
        fireTableDataChanged();
    }

    //one node has changed its status / tooltip
    public void nodeChanged(JxtaNode node) {
        fireTableDataChanged();
    }

}
